package com.beerandjesus.sportofkings.handicapper.dao.repository;

import com.beerandjesus.sportofkings.handicapper.dao.DAOException;
import com.beerandjesus.sportofkings.handicapper.dao.ProfileHistoryDAO;
import com.beerandjesus.sportofkings.handicapper.model.ProfileResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Date: 7/30/13
 * Time: 9:38 PM
 */
@Component
public class ProfileHistoryRepositoryImpl implements ProfileHistoryRepository {

    @Autowired
    private ProfileHistoryDAO profileHistoryDAO;

    public ProfileResult save(ProfileResult profileResult) {
        getProfileHistoryDAO().save(profileResult);
        return profileResult;
    }

    public ProfileResult find(ProfileResult profileResult) {
        return getProfileHistoryDAO().find(profileResult);
    }

    public int getProfileHistoryCount() {
        return getProfileHistoryDAO().getCount();
    }

    public void rebuildProfileHistoryDatabase() throws DAOException {
        try {
            // Facility to not rebuild the db if it's already there
            getProfileHistoryDAO().getCount();
        } catch (Exception e) {
            getProfileHistoryDAO().buildDatabase();
        }
    }

    public ProfileHistoryDAO getProfileHistoryDAO() {
        return profileHistoryDAO;
    }

    public void setProfileHistoryDAO(ProfileHistoryDAO profileHistoryDAO) {
        this.profileHistoryDAO = profileHistoryDAO;
    }
}
